package cn.ljlao.mall.product.goods.service.impl;


import cn.ljlao.common.utils.PageUtils;
import cn.ljlao.common.utils.Query;
import cn.ljlao.mall.product.goods.vo.AttrAttrgroupRelationVo;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



import cn.ljlao.mall.product.goods.dao.AttrAttrgroupRelationDao;
import cn.ljlao.mall.product.goods.entity.AttrAttrgroupRelationEntity;
import cn.ljlao.mall.product.goods.service.AttrAttrgroupRelationService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void saveBatch(List<AttrAttrgroupRelationVo> list) {
        List<AttrAttrgroupRelationEntity> relationEntityList = list.stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();

            relationEntity.setAttrId(item.getAttrId());
            relationEntity.setAttrGroupId(item.getAttrGroupId());
            return relationEntity;
        }).collect(Collectors.toList());

        this.saveBatch(relationEntityList);
    }


    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    @DS("master")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void ttt(){
        attrAttrgroupRelationDao.savesss();
    }

}